package com.views.structure {
	import com.View;
	import com.ViewBase;
	
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.geom.Point;
	import flash.utils.setTimeout;

	/**
	 *
	 * 客户端分层结构
	 *
	 *
	 */
	public class Structure {
		public static const minStageWidth:int = 1000;
		public static const minStageHeight:int = 600;
		public static const maxStageWidth:int = 1250;
		public static const maxStageHeight:int = 650;

		private static var _instance:Structure;
		private var _stage:Stage;
		private var _view:View;
		private var _mouse:Sprite;
		private var _tip:Tip;
		private var _load:Load;
		private var _alert:Alert;
		private var _content:Content;
		private var _background:Background;
		private var _popup:Popup;
		private var _debugLayer:Sprite;

		public function Structure(sin:Singleton) {

		}

		public function get popup():Popup {
			return this._popup;
		}

		public function get alert():Alert {
			return this._alert;
		}

		public function get tip():Tip {
			return this._tip;
		}

		public function get content():Content {
			return this._content;
		}

		private function get stage():Stage {
			return this._stage;
		}

		public function get mouse():Sprite {
			return this._mouse;
		}

		public function get debugLayer():Sprite {
			return this._debugLayer;
		}

		/**
		 * 获取视图层架构唯一实例
		 * @param	stage		舞台
		 * @param	viewBase	视图层基类引用
		 * @return
		 */
		public static function getInstance(stage:Stage, viewBase:ViewBase):Structure {
			if (null == Structure._instance) {
				Structure._instance = new Structure(new Singleton());
				Structure._instance.init(stage, (viewBase as View));
			}
			return Structure._instance;
		}

		private function init(stage:Stage, viewBase:View):void {
			this._stage = stage;
			this._view = viewBase;
			this._mouse = new Sprite();
			this._tip = new Tip(); //new Tip();
			this._load = new Load();
			this._alert = new Alert();
			this._popup = new Popup(this._view);
			this._content = new Content();
			this._debugLayer = new Sprite();

			this._background = new Background();
			this._stage.addChild(this._background);
			this._stage.addChild(this._content);
			this._stage.addChild(this._popup);
			this._stage.addChild(this._alert);
			this._stage.addChild(this._load);
			this._stage.addChild(this._tip);
			this._stage.addChild(this._mouse);
			this._stage.addChild(_debugLayer);

			this._popup.structure = this;
			this._mouse.mouseEnabled = false;
			this._mouse.mouseChildren = false;
			this._tip.mouseEnabled = false;
			this._popup.mouseEnabled = false;
			_debugLayer.mouseChildren = false;
			_debugLayer.mouseEnabled = false;

			this._content.tabChildren = this._popup.tabChildren = false;
			this._content.tabEnabled = this._popup.tabEnabled = false;
		}

		public function drawMark():void {
			this.drawBlack(-600, -200, 2500, 200);
			this.drawBlack(1250, 0, 1000, 1000);
			this.drawBlack(-600, 650, 2500, 200);
			this.drawBlack(-1000, 0, 1000, 1000);
		}

		private function drawBlack(_arg1:Number, _arg2:Number, _arg3:Number, _arg4:Number):void {
			this._tip.graphics.beginFill(0, 1);
			this._tip.graphics.drawRect(_arg1, _arg2, _arg3, _arg4);
			this._tip.graphics.endFill();
		}

		public static function get stageOffset():Point {
			var _offsetX:Number = 0;
			var _offsetY:Number = 0;
			if (_instance.stage.stageWidth > maxStageWidth) {
				_offsetX = (_instance.stage.stageWidth - maxStageWidth) / 2;
			}
			if (_instance.stage.stageHeight > maxStageHeight) {
				_offsetY = (_instance.stage.stageHeight - maxStageHeight) / 2;
			}
			return (new Point(Math.floor(_offsetX), Math.floor(_offsetY)));
		}

		public static function get stageWidth():Number {
			return ((Math.max(minStageWidth, Math.min(maxStageWidth, _instance.stage.stageWidth)) - stageClip.x));
		}

		public static function get stageHeight():Number {
			return ((Math.max(minStageHeight, Math.min(maxStageHeight, _instance.stage.stageHeight)) - stageClip.y));
		}

		public static function get stageClip():Point {
			var _local1:Number = 0;
			var _local2:Number = 0;
			if (_instance.stage.stageWidth < minStageWidth) {
				_local1 = (minStageWidth - Math.max(500, _instance.stage.stageWidth));
			}
			if (_instance.stage.stageHeight < minStageHeight) {
				_local2 = (minStageHeight - Math.max(400, _instance.stage.stageHeight));
			}
			return (new Point(Math.floor(_local1), Math.floor(_local2)));
		}

		public function reposition():void {
			this._mouse.x = this._tip.x = this._load.x = this._alert.x = this._popup.x = this._content.x = this._background.x = stageOffset.x;
			this._mouse.y = this._tip.y = this._load.y = this._alert.y = this._popup.y = this._content.y = this._background.y = stageOffset.y;
			setTimeout(this._popup.reposition, 10);
		}

	}

}

class Singleton {


}
